<?php
/**
 * @desc 文章管理
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class ArticleController extends Controller
{
	//过滤器
	public function filters()
    {
        return array(
            'UserLogin',
			'AjaxReq'
        );
    }

	//首页
	public function actionIndex()
	{
		$request = Yii::app()->request;
		$current = $request->getParam('pageNum');
		$numPerPage = $request->getParam('numPerPage');
		$name = $request->getParam('name');
		$page['current'] = empty($current)?1:$current;
		$page['numPerPage'] = empty($numPerPage)?Yii::app()->params['numPerPage']:$numPerPage;
		$page['name'] = $name;
		//数据查询
		$model = new Article;
		$list = $model->getArticleList($page);
		$count = array_pop($list);
		$page['total'] = $count[0];
		$this->render('index', array(
				'page' => $page,
				'list' => $list
			));
	}
	//添加文章
	public function actionAdd(){
		//获取参数
		$request = Yii::app()->request;
		$form = $request->getParam('dataForm');
		if(!empty($form)){
			$model = new ArticleForm('add');
			$model->attributes = $form;
			if(!$model->validate() || !$model->saveData()){
				$msg = array_values($model->getErrors());
				$this->output['status'] = 10000;
				$this->output['msg'] = $msg[0];
				$this->renderJson($this->output);
			}
			$this->output['code'] = 200;
			$this->output['navTabId'] = 'article';
			$this->output['callbackType'] = 'closeCurrent';
			$this->renderJson($this->output);
		}
		$this->render("add");
	}
	//删除文章
	public function actionDelete(){
		//获取参数
		$request = Yii::app()->request;
		$ids = $request->getParam('ids');
		//删除操作
		$model = new Article;
		$result = $model->deleteArticle($ids);
		if(empty($result)){
			$this->output['status'] = 10003;
			$this->renderJson($this->output);
		}
		$this->output['code'] = 200;
		$this->output['navTabId'] = 'article';
		$this->renderJson($this->output);
	}
	//修改用户
	public function actionEdit(){
		//获取参数
		$request = Yii::app()->request;
		$id = $request->getParam('id');
		//修改
		$form = $request->getParam('dataForm');
		if(!empty($form)){
			$model = new ArticleForm;
			$model->attributes = $form;
			if(!$model->validate() || !$model->editData($id)){
				$msg = array_values($model->getErrors());
				$this->output['status'] = 10000;
				$this->output['msg'] = empty($msg[0])?Yii::app()->params['any_write']:$msg[0];
				$this->renderJson($this->output);
			}
			$this->output['code'] = 200;
			$this->output['navTabId'] = 'article';
			$this->output['callbackType'] = 'closeCurrent';
			$this->renderJson($this->output);
		}
		//获取用户
		$model = new Article;
		$data = $model->getArticle($id);
		$data['content'] = htmlspecialchars_decode($data['content']);
		$this->render('edit', array(
				'data'=>$data
			));
	}
}